package com.hengxinyongli.iam.filter.impl;

import com.hengxinyongli.iam.filter.PermitsFilter;
import com.hengxinyongli.iam.filter.PermitsValidateChain;
import com.hengxinyongli.iam.utils.UriUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.Iterator;
import java.util.Set;

/**
 * @author magic
 * @date 2018/9/28/028 14:49
 * @version 1.0
 * Description AntMatcherFilter
 */
@Component
@Order(3)
public class AntMatcherFilter implements PermitsFilter {

    AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public void invoke(PermitsValidateChain chain,String uri,String method, Set<String> permits) {
        boolean hasPass = false;
        String validateUri = UriUtils.buildValidateUri(uri,method);
        Iterator<String> permitIterators = permits.iterator();
        while (permitIterators.hasNext()){
            String pattern = permitIterators.next();
            if(antPathMatcher.match(pattern,validateUri)){
                hasPass = true;
                break;
            }
        }
        if (!hasPass){
            chain.filter(uri,method,permits);
        }
    }
}
